package com.shijimo.generator.vue;

import com.shijimo.generator.util.DbUtil;
import com.shijimo.generator.util.Field;
import com.shijimo.generator.util.FreemarkerUtil;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.*;

/**
 * @author Dream_飞翔
 * @date 2021/10/9
 * @time 19:10
 * @email 1072876976@qq.com
 */
public class VueGenerator {

    // 模块名称
    static String MODULE = "file";
    // Dto文件生成路径
    static String toVuePath = "admin\\src\\views\\admin\\";

    // 读取mybatis-generator映射文件的所在地址
    static String generatorConfigPath = "server\\src\\main\\resources\\generator\\generatorConfig.xml";

    public static void main(String[] args) throws Exception {
        String module = MODULE;

        // 只生成配置文件中的第一个table节点
        File file = new File(generatorConfigPath);
        SAXReader reader = new SAXReader();

        //读取xml文件到Document中
        Document doc = reader.read(file);

        //获取xml文件的根节点
        Element rootElement = doc.getRootElement();

        //读取context节点
        Element contextElement = rootElement.element("context");
        //定义一个Element用于遍历
        Element tableElement;

        //取第一个“table”的节点
        tableElement = contextElement.elementIterator("table").next();
        String Domain = tableElement.attributeValue("domainObjectName");
        String tableName = tableElement.attributeValue("tableName");
        String tableNameCn = DbUtil.getTableComment(tableName);
        String domain = Domain.substring(0, 1).toLowerCase() + Domain.substring(1);
        System.out.println("表：" + tableElement.attributeValue("tableName"));
        System.out.println("Domain：" + tableElement.attributeValue("domainObjectName"));

        // 获取所有的列信息
        List<Field> fieldList = DbUtil.getColumnByTableName(tableName);
        // 获取所有的列类型
        Set<String> typeSet = getJavaTypes(fieldList);

        Map<String, Object> map = new HashMap<>();

        map.put("Domain", Domain);
        map.put("domain", domain);
        map.put("tableNameCn", tableNameCn);
        map.put("module", module);

        map.put("fieldList", fieldList);
        map.put("typeSet", typeSet);

        // 生成Vue文件
        FreemarkerUtil.initConfig("vue.ftl");
        FreemarkerUtil.generator(toVuePath + domain + ".vue", map);
    }

    /**
     * 获取所有的Java类型，使用Set去重
     *
     * @param fieldList 列信息集合
     * @return
     */
    private static Set<String> getJavaTypes(List<Field> fieldList) {
        Set<String> set = new HashSet<>();
        for (int i = 0; i < fieldList.size(); i++) {
            Field field = fieldList.get(i);
            set.add(field.getJavaType());
        }
        return set;
    }
}
